home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <events.h>
-
- #include "tcpglue.h"
-
- static UDPiopb pb,rpb,wpb;
-
- static xCleanup(status) int status;
- {
- (void) xUDPRelease(&pb);
- }
-
- /*
- * UDP asynchronous notification routine
- */
- static int notified = 0;
- static int lastNotifyCount = 0;
-
- static StreamPtr notifyUdpStream;
- static unsigned short notifyEventCode;
- static Ptr notifyUserDataPtr;
- static unsigned short notifyTerminReason;
- static struct ICMPReport *notifyIcmpMsg;
-
- pascal void sock_udp_notify(udpStream,eventCode,userDataPtr,icmpMsg)
- StreamPtr udpStream;
- unsigned short eventCode;
- Ptr userDataPtr;
- struct ICMPReport *icmpMsg;
- {
- notified++;
-
- notifyUdpStream = udpStream;
- notifyEventCode = eventCode;
- notifyUserDataPtr = userDataPtr;
- notifyIcmpMsg = icmpMsg;
- }
-
- static char *eventNames[] =
- {
- "event 0",
- "data arrival",
- "ICMP message"
- };
- static char *icmpMessages[] =
- {
- "net unreachable",
- "host unreachable",
- "protocol unreachable",
- "port unreachable",
- "fragmentation required",
- "source route failed",
- "time exceeded",
- "parameter problem",
- "missing required option"
- };
- udpCheckNotify()
- {
- if (notified == lastNotifyCount)
- return(0);
-
- lastNotifyCount = notified;
- dprintf("notify count is now %d\n",lastNotifyCount);
- dprintf("stream %08x\n",notifyUdpStream);
- dprintf("event %d '%s'\n",notifyEventCode,eventNames[notifyEventCode]);
- if (notifyEventCode == UDPDataArrival)
- dprintf("%d bytes\n",notifyTerminReason/*!?*/);
- dprintf("icmp msg %08x\n",notifyIcmpMsg);
- if (notifyEventCode == UDPICMPReceived)
- {
- dprintf("stream %08x\n",notifyIcmpMsg->streamPtr);
- dprintf("local %08x/%d\n",notifyIcmpMsg->localHost,notifyIcmpMsg->localPort);
- dprintf("remote %08x/%d\n",notifyIcmpMsg->remoteHost,notifyIcmpMsg->remotePort);
- dprintf("%s\n",icmpMessages[notifyIcmpMsg->reportType]);
- dprintf("optionalAddlInfo %04x\n",notifyIcmpMsg->optionalAddlInfo);
- dprintf("optionalAddlInfoPtr %08x\n",notifyIcmpMsg->optionalAddlInfoPtr);
- }
- dprintf("userdata %s\n",notifyUserDataPtr);
- return(1);
- }
-
- /*
- * UDP io completion routine
- */
- static OSErr UDPResult;
- static long rhost;
- static short rport;
- static int len;
-
- static void UDPDone(pb)
- struct UDPiopb *pb;
- {
- UDPResult = pb->ioResult;
- if (UDPResult != noErr)
- return;
-
- switch (pb->csCode)
- {
- case UDPRead:
- len = pb->csParam.receive.rcvBuffLen;
- break;
- }
- }
-
- static OSErr io;
- static char rcvbuf[1024];
- static char sendbuf[1024];
- wdsEntry wds[2];
-
- main()
- {
- atexit(xCleanup);
-
- if ((io = xUDPCreate(8192, sock_udp_notify, 0, &pb)) != noErr)
- {
- fprintf(stderr,"UDPCreate failed code %d\n",io);
- exit(1);
- }
- fprintf(stderr,"udpStream %08x\n",pb.udpStream);
-
- udpCheckNotify();
-
- #if 0
- rpb.udpStream = pb.udpStream;
- if ((io = xUDPRead(&rpb, -1L)) != noErr)
- {
- fprintf(stderr,"xUDPRead failed code %d\n",io);
- exit(1);
- }
-
- udpCheckNotify();
- #endif
-
- wpb.udpStream = pb.udpStream;
- strcpy(sendbuf,"HELO milligan.utcs.utoronto.ca");
- wds[0].length = strlen(sendbuf);
- wds[0].ptr = sendbuf;
- wds[1].length = 0;
- if ((io = xUDPWrite(&wpb, 0x8064660a,69, &wds[0], 0L)) != noErr)
- {
- fprintf(stderr,"xUDPWrite failed code %d\n",io);
- exit(1);
- }
-
- for(;;)
- {
- udpCheckNotify();
- }
-
- if ((io = xUDPRelease(&pb)) != noErr)
- {
- fprintf(stderr,"xUDPRelease failed code %d\n",io);
- exit(1);
- }
- }
-
-